const BlogService = require("../../services/admin/BlogService")

const BlogController = {
  add: async (req, res) => {
    const { title, content, category, isPublish } = req.body
    const cover = req.file ? `/coveruploads/${req.file.filename}` : ''
    await BlogService.add({
      title,
      content,
      category: Number(category),
      cover,
      isPublish: Number(isPublish),
      updateTime: new Date()
    })
    res.send({
      ActionType: 'OK'
    })
  },
  list: async (req, res) => {
    const blogList = await BlogService.list({ _id: req.params.id })
    res.send({
      ActionType: 'OK',
      data: blogList
    })
  },
  publish: async (req, res) => {
    await BlogService.publish({
      ...req.body,
      updateTime: new Date()
    })
    res.send({
      ActionType: 'OK'
    })
  },
  delete: async (req, res) => {
    await BlogService.delete({ _id: req.params.id })
    res.send({
      ActionType: 'OK'
    })
  },
  update: async (req, res) => {
    const { title, content, category, isPublish,_id } = req.body
    const cover = req.file ? `/coveruploads/${req.file.filename}` : ''
    await BlogService.update({
      _id,
      title,
      content,
      category: Number(category),
      cover,
      isPublish: Number(isPublish),
      updateTime: new Date()
    })
    res.send({
      ActionType: 'OK'
    })
  }
}

module.exports = BlogController